package model

import (
	"github.com/dgrijalva/jwt-go"
	"time"
)

var (
	secret = []byte("zjjMyLove")
)

type InfoClaim struct {
	jwt.StandardClaims
	Uuid   string `json:"uuid"`
	UserId uint   `json:"user_id"`
}

func (ic *InfoClaim) CheckJwtToken() (string, bool) {
	return "", true
}

func (ic *InfoClaim) GenerateJwtToken() string {
	nowTimeStamp := time.Now().Unix()
	ic.IssuedAt = nowTimeStamp
	ic.ExpiresAt = nowTimeStamp + 1800
	token := jwt.NewWithClaims(jwt.SigningMethodHS256, ic)
	tokenString, _ := token.SignedString(secret)
	return tokenString
}

func (ic *InfoClaim) ParseJwtToken(tokenString string) bool {
	if _, err := jwt.ParseWithClaims(tokenString, ic, keyFunc); err != nil {
		return false
	}
	return true
}

func keyFunc(token *jwt.Token) (interface{}, error) {
	return secret, nil
}
